//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.Resource.Properties;

namespace EntLibContrib.Resource.Configuration.Design
{
	/// <summary>
	/// Represents the root configuration for the Resource Application Block.
	/// </summary>
	[Image(typeof(ResourceSettingsNode))]
	[SelectedImage(typeof(ResourceSettingsNode))]
	public sealed class ResourceSettingsNode : ConfigurationSectionNode
	{
		#region Fields
		private ResourceManagerBaseNode defaultResourceManagerNode;
		private EventHandler<ConfigurationNodeChangedEventArgs> onDefaultResourceManagerNodeRemoved;
		#endregion

		#region Properties
		/// <summary>
		/// <para>Gets the name for the node.</para>
		/// </summary>
		/// <value>
		/// <para>The display name for the node.</para>
		/// </value>		
		[ReadOnly(true)]
		public override string Name
		{
			get { return base.Name; }
		}

		/// <summary>
		/// Gets or sets the default Resource manager
		/// </summary>
		/// <value>
		/// The default Resource manager.
		/// </value>
		[Required]
		[Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
		[ReferenceType(typeof(ResourceManagerBaseNode))]
		[ResourceCategory("CategoryGeneral")]
		[ResourceDescription("DefaultResourceManagerDescription")]
		public ResourceManagerBaseNode DefaultResourceManager
		{
			get { return defaultResourceManagerNode; }
			set
			{
				defaultResourceManagerNode = LinkNodeHelper.CreateReference<ResourceManagerBaseNode>(defaultResourceManagerNode,
																																														value,
																																														onDefaultResourceManagerNodeRemoved,
																																														null);
			}
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ResourceSettingsNode"/> class.
		/// </summary>		
		public ResourceSettingsNode ()
			: base(InternalResources.DefaultResourceSettingsNodeName)
		{
			this.onDefaultResourceManagerNodeRemoved = new EventHandler<ConfigurationNodeChangedEventArgs>(OnDefaultResourceManagerNodeRemovedHandler);
		}
		#endregion

		#region Protected Override Methods
		/// <summary>
		/// Releases the unmanaged resources used by the <see cref="ResourceSettingsNode "/> and optionally releases the managed resources.
		/// </summary>
		/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
		protected override void Dispose (bool disposing)
		{
			try
			{
				if ( disposing )
				{
					if (null != defaultResourceManagerNode)
					{
						defaultResourceManagerNode.Removed -= onDefaultResourceManagerNodeRemoved;
					}
				}
			}
			finally
			{
				base.Dispose(disposing);
			}
		}
		#endregion

		#region Private Events
		private void OnDefaultResourceManagerNodeRemovedHandler(object sender, ConfigurationNodeChangedEventArgs e)
		{
			this.defaultResourceManagerNode = null;
		}
		#endregion
	}
}